#include "PowerBar.h"

// Initialize the instance to null
PowerBar* PowerBar::m_PowerBarInstance = 0;

PowerBar::PowerBar(void)
{
}

PowerBar::~PowerBar(void)
{
}

PowerBar* PowerBar::GetInstance(void)
{
	// Create the singleton if it doesn't exist
	if(!m_PowerBarInstance)
		m_PowerBarInstance = new PowerBar;

	// Return the singleton
	return m_PowerBarInstance;
}

void PowerBar::DeletePowerBarInstance(void)
{
	// If instance exist, delete it!
	if(m_PowerBarInstance)
	{
		delete m_PowerBarInstance;
		m_PowerBarInstance = NULL;
	}
}

void PowerBar::Initialize(void)
{
	// Get the instance and initialize the sprite manager
	m_pSpriteManager = SpriteManager::GetInstance();
	
	iPowerLevel = 70;
	sCheckpointPower = 70;
}

void PowerBar::LoadPowerBar(void)
{
	// Power bar testing
	sPowerBarFull = m_pSpriteManager->LoadSprite("UISprites\\PowerBar0.png", D3DXVECTOR3(566, 648, 0), 0.75f);
	sPowerBar1 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar1.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBar2 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar2.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBar3 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar3.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBar4 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar4.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBar5 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar5.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBar6 = m_pSpriteManager->LoadSprite("UISprites\\PowerBar6.png", D3DXVECTOR3(565, 648, 0), 0.7f);
	sPowerBarEmpty = m_pSpriteManager->LoadSprite("UISprites\\PowerBar7.png", D3DXVECTOR3(565, 648, 0), 0.7f);

	sPowerBarGuage = sPowerBarFull;
}

void PowerBar::Update()
{
	//depending on power level, render the corresponding power bar sprite
	if(iPowerLevel == 70)
		sPowerBarGuage = sPowerBarFull;
	if(iPowerLevel <= 60)
		sPowerBarGuage = sPowerBar1;
	if(iPowerLevel <= 50)
		sPowerBarGuage = sPowerBar2;
	if(iPowerLevel <= 40)
		sPowerBarGuage = sPowerBar3;
	if(iPowerLevel <= 30)
		sPowerBarGuage = sPowerBar4;
	if(iPowerLevel <= 20)
		sPowerBarGuage = sPowerBar5;
	if(iPowerLevel <= 10)
		sPowerBarGuage = sPowerBar6;
	if(iPowerLevel == 0)
		sPowerBarGuage = sPowerBarEmpty;
}

int PowerBar::CheckPowerLevel()
{
	return (int)iPowerLevel;
}

void PowerBar::ChangePowerLevel(int iPowerChange)
{
	iPowerLevel += iPowerChange;
	if(iPowerLevel > 70)
		iPowerLevel = 70;
}

void PowerBar::ResetPower()
{
	iPowerLevel = sCheckpointPower;
}

void PowerBar::SetCheckpoint(void)
{
	sCheckpointPower = iPowerLevel;
}

void PowerBar::FullPower()
{
	// Reset power bar to max 70
	iPowerLevel = 70;
}

void PowerBar::Render(void)
{
	m_pSpriteManager->DrawSprite(sPowerBarGuage);
}

void PowerBar::DestroyPower(void)
{
	for(short i = sPowerBarEmpty; i >= sPowerBarFull; i--)
		m_pSpriteManager->DestroySprite(i);
}